package com.zyf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zyf.pojo.DrugInfo;
import com.zyf.service.DrugInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DrugInfoController {

    //注入service
    @Autowired
    private DrugInfoService drugInfoService;

    /*处理药品列表查询请求*/
    @GetMapping("/queryDrugInfoList")
    public List<DrugInfo> queryDrugList(String drugName, String mnemonicCode){
        QueryWrapper<DrugInfo> wrapper=new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(drugName),"drug_name",drugName);
        wrapper.like(!StringUtils.isEmpty(mnemonicCode),"mnemonic_code",mnemonicCode);

        List<DrugInfo> list = drugInfoService.list(wrapper);
        return list;
    }

    /*处理患者抓取药品列表的请求*/

    @GetMapping("/queryRegDurgList")
    public List<DrugInfo> queryRegDrugList(Integer registerId){
        return drugInfoService.queryDrugRegisterListService(registerId);
    }

    /*处理药房发药请求*/
    @RequestMapping("/sendDrugInfo")
    public Map<String,Object> sendDrugInfo(Integer pid, Integer did, Integer drugNumber){

        Map<String,Object> result=new HashMap<>();
        try{
            drugInfoService.updatePreDrugService(pid,did,drugNumber);
            result.put("code",200);
            result.put("msg","药房发药成功....");

        }catch(Exception ex){
            result.put("code",400);
            result.put("msg","药房发药失败....");
        }
        return result;
    }

    /*处理药品信息分页查询请求*/
    @RequestMapping("/queryDrugPage")
    public Map<String,Object> queryDrugPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody  DrugInfo drugInfo){

        return drugInfoService.queryDurgPageService(pageNum,pageSize,drugInfo);

    }

    /*添加药品信息*/
    @PostMapping("/saveDrugInfo")
    public Map<String,Object> saveDrugInfo(@RequestBody DrugInfo drugInfo){
        Map<String,Object> result=new HashMap<>();
        try{
            drugInfoService.save(drugInfo);
            result.put("code",200);
            result.put("msg","药品添加成功....");

        }catch(Exception ex){
            result.put("code",400);
            result.put("msg","药品添加失败....");
        }
        return result;
    }

    /*处理药品信息更新请求*/
    @PostMapping("/updateDrugInfo")
    public Map<String,Object> updateDrugInfo(@RequestBody DrugInfo drugInfo){
        Map<String,Object> result=new HashMap<>();
        try{
            drugInfoService.updateById(drugInfo);
            result.put("code",200);
            result.put("msg","药品更新成功....");

        }catch(Exception ex){
            result.put("code",400);
            result.put("msg","药品更新失败....");
        }
        return result;
    }
}
